package com.example.android_wx.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.android_wx.R;
import com.example.android_wx.activity.LoginActivity;
import com.example.android_wx.activity.MainActivity;
import com.example.android_wx.activity.ModifyGenderActivity;
import com.example.android_wx.activity.ModifyMobileActivity;
import com.example.android_wx.activity.ModifyNameActivity;
import com.example.android_wx.activity.ModifyPasswordActivity;
import com.example.android_wx.activity.MyCircleList;
import com.example.android_wx.activity.Personalinformation;
import com.example.android_wx.base.App;
import com.example.android_wx.base.GlideEngine;
import com.example.android_wx.http.BaseCallBack;
import com.example.android_wx.http.HttpHelp;
import com.example.android_wx.http.MemberData;
import com.google.gson.Gson;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.io.File;
import java.util.List;


public class MineFragment extends Fragment {
    private AppCompatTextView nicknameTextView;
    private AppCompatTextView idTextView;
    private AppCompatImageView avatarImageView;
    private AppCompatTextView logoutTextView;
    private AppCompatTextView modifyPasswordTextView, modifyNicknameTextView, modifyMobileTextView, modifyGenderTextView, modifySetTextView, MyCircleTextview;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_mine, container, false);
        nicknameTextView = root.findViewById(R.id.nicknameTextView);
        idTextView = root.findViewById(R.id.idTextView);
        logoutTextView = root.findViewById(R.id.logoutTextView);
        avatarImageView = root.findViewById(R.id.avatarImageView);
        modifyPasswordTextView = root.findViewById(R.id.modifyPasswordTextView);
        modifyNicknameTextView = root.findViewById(R.id.modifyNicknameTextView);
        modifyMobileTextView = root.findViewById(R.id.modifyMobileTextView);
        modifyGenderTextView = root.findViewById(R.id.modifyGenderTextView);
        modifySetTextView = root.findViewById(R.id.modifySetTextView);
        MyCircleTextview = root.findViewById(R.id.MyCircleTextview);
        initEvent();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();

        //   nicknameTextView.setText(App.get().getMemberData().getMemberNickname());
        nicknameTextView.setText(App.get().getMemberData().getMemberNickname());
        idTextView.setText("手机号：");
        //   idTextView.append(App.get().getMemberData().getMemberMobile());
        idTextView.setText(App.get().getMemberData().getMemberMobile());
        System.out.println(App.get().getMemberData().getMemberAvatar());
        Glide.with(this).load(App.get().getMemberData().getMemberAvatar()).into(avatarImageView);


    }


    private void initEvent() {

        modifySetTextView.setOnClickListener(view -> startActivity(new Intent(getActivity(), Personalinformation.class)));
        modifyPasswordTextView.setOnClickListener(view -> startActivity(new Intent(getActivity(), ModifyPasswordActivity.class)));
        modifyNicknameTextView.setOnClickListener(view -> startActivity(new Intent(getActivity(), ModifyNameActivity.class)));
        modifyMobileTextView.setOnClickListener(view -> startActivity(new Intent(getActivity(), ModifyMobileActivity.class)));
        modifyGenderTextView.setOnClickListener(view -> startActivity(new Intent(getActivity(), ModifyGenderActivity.class)));
        MyCircleTextview.setOnClickListener(view -> startActivity(new Intent(getActivity(), MyCircleList.class)));
//        //订阅键名为logout的字符消息
//        LiveEventBus.get("some_key", String.class).observe(this, s -> {
//            //接收到消息之后的代码操作
//
//            nicknameTextView.setText(s+"");
//
//        });


        avatarImageView.setOnClickListener(v -> {
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(List<LocalMedia> result) {
                            System.out.println(result.get(0).getPath());
                            // 结果回调
                            HttpHelp.get().ready("member", "modifyAvatar")
                                    .post(new File(result.get(0).getPath()), new BaseCallBack() {
                                        @Override
                                        public void onSuccess(String data) {
                                            App.get().getMemberData().setMemberAvatar(data);
                                            Glide.with(MineFragment.this).load(data).into(avatarImageView);


                                        }

                                        @Override
                                        public void onFailure(String reason) {

                                        }
                                    });

                        }

                        @Override
                        public void onCancel() {

                        }
                    });


        });

        logoutTextView.setOnClickListener(view -> {
            new AlertDialog.Builder(getActivity())
                    .setTitle("确认您的选择~")
                    .setMessage("注销登录？")
                    .setCancelable(false)//此属性确定是否可以点击其他区域关闭对话
                    .setPositiveButton("确定", (dialogInterface, i) -> {
                        logoutTextView.setEnabled(false);
                        logoutTextView.setText("正在注销...");
                        HttpHelp.get().ready("member", "logout")
                                .get(new BaseCallBack() {
                                    @Override
                                    public void onSuccess(String data) {
                                        Toast.makeText(getActivity(), "注销成功", Toast.LENGTH_SHORT).show();
                                        App.get().getEditor().putString("token", "");
                                        App.get().getEditor().commit();

                                        LiveEventBus.get("logout").post("success");
                                    }

                                    @Override
                                    public void onFailure(String reason) {
                                        Toast.makeText(getActivity(), reason, Toast.LENGTH_SHORT).show();
                                        logoutTextView.setEnabled(true);
                                        logoutTextView.setText("注销登录");


                                    }
                                });


                    })
                    .setNegativeButton("取消", (dialogInterface, i) -> {

                    })
                    .show();
        });

    }

    private void getMemberData() {

        HttpHelp.get().ready("member", "getMemberData")
                .get(new BaseCallBack() {
                    @Override
                    public void onSuccess(String data) {

                    }

                    @Override
                    public void onFailure(String reason) {


                    }
                });

    }

}


